//========================================================================
/**
 * Author   : cuisw <shaovie@gmail.com>
 * Date     : 2008-11-05 22:43
 */
//========================================================================

#ifndef _WIN32PROACTOR_H_
#define _WIN32PROACTOR_H_
#include "Pre.h"

/**
 * @class Win32Proactor
 *
 * @brief A manager for asynchronous event demultiplexing on Win32.
 */
class Win32Proactor : public ProactorImpl
{
public:
    //
    Win32Proactor (size_t number_of_threads = 0);
    
    //
    virtual ~Win32Proactor ();

    //
    int open (size_t number_of_threads = 0);

    int close (void);

    // This method adds the @a handle to the I/O completion port. This
    // function is a no-op function for Unix systems.
    virtual int register_handle (NDK_HANDLE handle,
	    const void *completion_key,
	    int event_mask);

    /**
     * Dispatch a single set of events. If <time_out> elapses
     * before any events occur, return 0. Return 1 if a completion
     * dispatched. Return -1 on errors.
     */
    virtual int handle_events (const TimeValue *timeout = 0);

    //
    virtual int run_event_loop (void);

    //
    virtual void end_event_loop (void);

    //
    virtual int event_loop_done (void);

    // = Methods used to create Asynch IO factory and result objects. We
    // create the right objects here in these methods.
    virtual AsynchReadStreamImpl *create_asynch_read_stream (void);
    virtual AsynchReadStreamResultImpl *create_asynch_read_stream_result (
	    const AsynchHandler *handler,
	    NDK_HANDLE handle, 
	    MessageBlock *mb,
	    size_t bytes_to_read);

    //
    virtual AsynchWriteStreamImpl *create_asynch_write_stream (void);
    virtual AsynchWriteStreamResultImpl *create_asynch_write_stream_result (
	    const AsynchHandler *handler,
	    NDK_HANDLE handle,
	    MessageBlock *mb,
	    size_t bytes_to_write);

    //
    virtual AsynchAcceptImpl *create_asynch_accept (void);
    virtual AsynchAcceptResultImpl *create_asynch_accept_result (
	    const AsynchHandler *handler,
	    NDK_HANDLE listen_handle,
	    MessageBlock *mb,
	    size_t num_to_accept);

    virtual size_t number_of_threads (void) const;

    virtual void number_of_threads (size_t threads);
private:
    // Handle for the completion port. 
    NDK_HANDLE completion_port_;

    // This number is passed to the @c CreateIOCompletionPort system
    // call.
    DWORD number_of_threads_;
};

#include "Post.h"
#endif

